---
AWSTemplateFormatVersion: "2010-09-09"
Description: 'EKS Node Group definition.'

Parameters:

  ClusterName:
    Type: String
    Description: The EKS cluster name.

  NodeGroupName:
    Type: String
    Description: Unique identifier for the Node Group.

  NodeInstanceType:
    Type: String
    Default: t3.medium
    Description: EC2 instance type for the node instances.

  NodeGroupDesiredCapacity:
    Type: Number
    Default: 3
    Description: Desired capacity of Node Group ASG.

  Subnets:
    Type: "List<AWS::EC2::Subnet::Id>"
    Description: The subnets where workers can be created.

Resources:

  NodeInstanceRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - ec2.amazonaws.com
            Action:
              - "sts:AssumeRole"
      ManagedPolicyArns:
        - "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
        - "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
        - "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
      Path: /

  NodeGroup:
    Type: 'AWS::EKS::Nodegroup'
    Properties:
      NodegroupName: !Ref NodeGroupName
      ClusterName: !Ref ClusterName
      NodeRole: !GetAtt NodeInstanceRole.Arn
      InstanceTypes:
        - !Ref NodeInstanceType
      ScalingConfig:
        MinSize: 2
        DesiredSize: !Ref NodeGroupDesiredCapacity
        MaxSize: 5
      Subnets: !Ref Subnets
